package com.java110.mall.api.importData.adapt;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.ValidateResult;
import com.java110.dto.data.ImportProduct;
import com.java110.dto.data.ImportProductExtand;
import com.java110.mall.api.importData.DefaultImportDataAdapt;
import com.java110.mall.api.importData.IImportDataCleaningAdapt;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ImportExcelUtils;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品数据导入处理类
 * <p>
 * 导入请求参数中必须包含
 * param.append('importAdapt', "importRoomOwner");
 */
@Service("importProductDataCleaning")
public class ImportProductDataCleaningAdapt extends DefaultImportDataAdapt implements IImportDataCleaningAdapt {

    private final static Logger logger = LoggerFactory.getLogger(ImportProductDataCleaningAdapt.class);

    @Override
    public List analysisExcel(Workbook workbook, JSONObject paramIn, ValidateResult result) throws Exception {

        List<ImportProduct> products = new ArrayList<ImportProduct>();
        List<ImportProductExtand> productExtands = new ArrayList<ImportProductExtand>();
        //获取楼信息
        getProducts(workbook, products, result);

        //获取业主信息
        getProductExtands(workbook, productExtands);
        //数据校验
        importExcelDataValidate(products, productExtands);

        if (ListUtil.isNull(products)) {
            throw new IllegalArgumentException("未包含数据");
        }
        List<ImportProductExtand> importProductExtands = null;
        for (ImportProduct importProduct : products) {
            importProductExtands = new ArrayList<>();
            for (ImportProductExtand importProductExtand : productExtands) {
                if (!importProduct.getProductId().equals(importProductExtand.getProductId())) {
                    continue;
                }

                importProductExtands.add(importProductExtand);
            }
            importProduct.setImportProductExtands(importProductExtands);
        }

        return products;
    }

    private void getProducts(Workbook workbook, List<ImportProduct> importProducts, ValidateResult result) {
        Sheet sheet = null;
        sheet = ImportExcelUtils.getSheet(workbook, "商品信息");
        List<Object[]> oList = ImportExcelUtils.listFromSheet(sheet);
        ImportProduct importProduct = null;
        for (int osIndex = 0; osIndex < oList.size(); osIndex++) {
            Object[] os = oList.get(osIndex);
            if (osIndex == 0) { // 第一行是 头部信息 直接跳过
                continue;
            }

            if (StringUtil.isNullOrNone(os[0])) {
                continue;
            }

            Assert.hasValue(os[0], "商品信息选项中" + (osIndex + 1) + "行商品序号为空");
            Assert.hasValue(os[1], "商品信息选项中" + (osIndex + 1) + "行商品名称为空");
            Assert.hasValue(os[2], "商品信息选项中" + (osIndex + 1) + "行商品分组为空");
            Assert.hasValue(os[3], "商品信息选项中" + (osIndex + 1) + "行单位为空");
            Assert.hasValue(os[4], "商品信息选项中" + (osIndex + 1) + "行关键词为空");
            Assert.hasValue(os[5], "商品信息选项中" + (osIndex + 1) + "行仓库为空");
            Assert.hasValue(os[6], "商品信息选项中" + (osIndex + 1) + "行库存告警为空");
            Assert.hasValue(os[7], "商品信息选项中" + (osIndex + 1) + "行商品状态为空");
            importProduct = new ImportProduct();
            importProduct.setProductId(os[0].toString());
            importProduct.setProdName(os[1].toString());
            importProduct.setCategoryName(os[2].toString());
            importProduct.setUnitName(os[3].toString());
            importProduct.setKeyword(os[4].toString());
            importProduct.setShName(os[5].toString());
            importProduct.setWarnStock(os[6].toString());
            importProduct.setStateName(os[7].toString());
            importProduct.setStoreId(result.getStoreId());
            importProduct.setUserId(result.getUserId());
            importProduct.setShopId(result.getShopId());
            importProducts.add(importProduct);
        }
    }

    /**
     * 获取 房屋信息
     *
     * @param workbook
     */
    private void getProductExtands(Workbook workbook, List<ImportProductExtand> productExtands) {
        Sheet sheet = null;
        sheet = ImportExcelUtils.getSheet(workbook, "商品扩展信息");
        List<Object[]> oList = ImportExcelUtils.listFromSheet(sheet);
        ImportProductExtand productExtand = null;
        for (int osIndex = 0; osIndex < oList.size(); osIndex++) {
            try {
                Object[] os = oList.get(osIndex);
                if (osIndex == 0) { // 第一行是 头部信息 直接跳过
                    continue;
                }
                if (StringUtil.isNullOrNone(os[0])) {
                    continue;
                }
                Assert.hasValue(os[0], "商品扩展信息选项中" + (osIndex + 1) + "行商品序号为空");
                Assert.hasValue(os[1], "商品扩展信息选项中" + (osIndex + 1) + "行规格为空");
                Assert.hasValue(os[2], "商品扩展信息选项中" + (osIndex + 1) + "行库存为空");
                Assert.hasValue(os[3], "商品扩展信息选项中" + (osIndex + 1) + "行销量为空");
                Assert.hasValue(os[4], "商品扩展信息选项中" + (osIndex + 1) + "行金额/积分为空");
                Assert.hasValue(os[5], "商品扩展信息选项中" + (osIndex + 1) + "行成本价为空");
                Assert.hasValue(os[6], "商品扩展信息选项中" + (osIndex + 1) + "行会员价格为空");
                Assert.hasValue(os[7], "商品扩展信息选项中" + (osIndex + 1) + "行市场价为空");
                //Assert.hasValue(os[8], "商品扩展信息选项中" + (osIndex + 1) + "行条形码为空");

                productExtand = new ImportProductExtand();
                productExtand.setProductId(os[0].toString());
                productExtand.setSpecName(os[1].toString());
                productExtand.setStock(os[2].toString());
                productExtand.setSales(os[3].toString());
                productExtand.setPrice(os[4].toString());
                productExtand.setCostPrice(os[5].toString());
                productExtand.setVipPrice(os[6].toString());
                productExtand.setOtPrice(os[7].toString());
                if (os.length > 8 && !StringUtil.isNullOrNone(os[6])) {
                    productExtand.setBarCode(os[8].toString());
                }
                productExtands.add(productExtand);
            } catch (Exception e) {
                logger.error("商品扩展信息校验失败", e);
                throw new IllegalArgumentException("商品扩展信息sheet中第" + (osIndex + 1) + "行数据错误，请检查" + e.getLocalizedMessage());
            }
        }
    }

    /**
     * 数据校验处理
     */
    private void importExcelDataValidate(List<ImportProduct> products, List<ImportProductExtand> productExtands) {

    }


}
